<!--
 * @Description: 设备模板管理页面
-->
<template>
  <BaseMainSlot>
    <!-- <template v-slot:btn-box>
      <ActionBar
        :edit-active="editActive"
        :has-del="false"
        :has-add="false"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @cancel="cancel"
        @edit="edit"
        @save="save"
      />
    </template> -->
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        class="tableData"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="equipmentTypeName" label="设备类型名称" />
        <el-table-column width="100" label="操作">
          <template v-if="scope.row.equipmentTypeId" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
            ><i class="el-icon-edit-outline"></i> 修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '设备模板信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="设备类型名称:" class="w100p">
            <el-select
              v-model="formData.equipmentTypeName"
              disabled
              filterable
              clearable
            >
              <el-option
                v-for="item in ipmentType"
                :key="item.equipmentTypeId"
                :label="item.equipmentTypeName"
                :value="item.equipmentTypeId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="">
            <el-table
              ref="rightTableData"
              :data="rightTableData"
              border
              stripe
              @selection-change="selectRow"
            >
              <!-- <el-table-column type="selection" width="45" align="center" />
              <el-table-column
                label="序号"
                type="index"
                width="50"
                align="center"
              /> -->
              <!-- <el-table-column v-if="nowChanceStau !== 'edit'" label="参数名称" prop="paramName" /> -->
              <el-table-column label="参数名称" prop="paramName">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.paramName"
                    :disabled="!canInput"
                  />
                </template>
              </el-table-column>
              <!-- <el-table-column v-if="nowChanceStau !== 'edit'" label="参数值" prop="paramValue" /> -->
              <el-table-column label="参数字段" prop="paramValue">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.paramValue"
                    :disabled="!canInput"
                  />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="80">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    style="color: red; text-decoration: underline"
                    :disabled="!canInput"
                    size="small"
                    @click.native.prevent="
                      deleteRow(scope.$index, rightTableData)
                    "
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>

          <el-form-item>
            <el-button :disabled="!canInput" class="buttons" @click="addRow">
              添加
            </el-button>
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
// import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import EquipmentTemplate from "@/api/basic-info-manage/equipment-template-manage";
import BaseMainSlot from "@/components/BaseMainSlot/basezuoyou.vue";
import { mymixin } from "@/utils/mixin";
export default {
  name: "",
  components: {
    // ActionBar,
    Pagination,
    BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      userName: "",
      rules: {},
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      selectedStaffList: [],
      equipmentTypeTemplateId: "",
      ipmentType: [],
      rightTableData: [],
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {
    rightTableData: {
      deep: true,
      handler(val) {
        val.forEach((item) => {
          if (item.paramValue) {
            let reg = "^[\u4e00-\u9fa5]+$";
            var exp = new RegExp(reg, "g");
            // console.log(item['paramValue'].replace(exp, " "))
            item["paramValue"] = item["paramValue"].replace(exp, "");
            return item["paramValue"];
          }
        });
      }
    }
  },
  created() {
    //  表格数据
    this.getTableDataList();
    this.getipmentTypeList();
  },
  mounted() {},
  methods: {
    // 获取表格数据
    getTableDataList() {
      this.rightTableData = [];
      this.loading = true;
      let page = this.currentPage;
      let size = this.pageSize;
      EquipmentTemplate.getAgriMateTypeList(page, size).then((res) => {
        this.loading = false;
        let data = res.data;
        this.totalSize = data.totalElements;
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    getipmentTypeList() {
      EquipmentTemplate.getipmentTypeList().then((res) => {
        let data = res.data;
        this.ipmentType = data;
      });
    },
    check_num() {
      this.num = this.num.replace(/[^\a-\z\A-\Z0-9]/g, "");
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      // console.log(row);
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.rightTableData = this.formData.equipmentTypeTemplates;

      // this.addActive = true;
      // this.addActive = false;
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.$refs.formData.resetFields();
      this.openDialog("edit");
    },
    handleSelectionChange(list) {
      this.equipmentTypeIdList = [];
      this.equipmentTypeIdList = list.map((item) => item.equipmentTypeId);
      if (this.equipmentTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    selectRow(val) {
      this.selectlistRow = val;
    },
    // 增加行
    addRow() {
      var list = {
        equipmentTypeTemplateId: this.equipmentTypeTemplateId,
        paramName: "",
        paramValue: ""
      };
      this.rightTableData.unshift(list);
      this.equipmentTypeTemplateId += 1;
    },
    // 删除改行
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    // 删除方法
    // 删除选中行
    delData() {
      for (let i = 0; i < this.selectlistRow.length; i++) {
        let val = this.selectlistRow;
        // 获取选中行的索引的方法
        // 遍历表格中tableData数据和选中的val数据，比较它们的equipmentTypeTemplateId,相等则输出选中行的索引
        // equipmentTypeTemplateId的作用主要是为了让每一行有一个唯一的数据，方便比较，可以根据个人的开发需求从后台传入特定的数据
        val.forEach((val, index) => {
          this.rightTableData.forEach((v, i) => {
            if (val.equipmentTypeTemplateId === v.equipmentTypeTemplateId) {
              // i 为选中的索引
              this.rightTableData.splice(i, 1);
            }
          });
        });
      }
      // 删除完数据之后清除勾选框
      this.$refs.rightTableData.clearSelection();
    },
    delmessage() {},
    save() {
      if (this.nowChanceStau === "edit") {
        // if (this.rightTableData.equipmentTypeTemplateId) {
        //   delete this.rightTableData.equipmentTypeTemplateId;
        // }
        this.formData.organizationNo = sessionStorage.organizationNo;
        this.formData.equipmentTypeTemplates = this.rightTableData;
        EquipmentTemplate.editAgriMateTypeInfo(this.formData).then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg || "修改成功！");
            this.closeDialog();
            this.getTableDataList();
            this.resetActionBar();
          } else {
            this.$message.error(res.msg || "修改失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
.buttons {
  background-color: rgba(255, 255, 255, 0);
  border-color: $inputBorderColor;
  color: $appMainTextColor;
  width: 100%;
}

::v-deep .buttons.is-disabled,
.buttons.is-disabled:focus,
.buttons.is-disabled:hover {
  color: #c0c4cc;
  cursor: not-allowed;
  background-image: none;
  background-color: #f0f0f060;
  border-color: #ebeef5;
}
.buttonBox {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-around;
}
::v-deep .el-form {
  .el-table {
    // width: 200px !important;
    .el-table__header-wrapper {
      // height: 20px !important;
      .el-table__header {
        height: 100% !important;
        .cell {
          font-size: 14px !important;
        }
      }
    }
    ::v-deep .el-table--medium th {
      padding: 0 !important;
    }
  }
}
</style>
